package com.kuaiditu.assistant.view.topbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kuaiditu.assistant.R;


/**
 * Created by Eenie on 2016/8/31 17:08.
 * Des：页面顶部的
 */

public class TopBar extends FrameLayout {
    private Context mContext;
    private Toolbar mToolbar;
    private TextView tvTitle;

    public TopBar(Context context) {
        this(context, null);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        if (attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.TopBar);
            int titleColor = a.getColor(R.styleable.TopBar_titleColor, mContext.getResources().getColor(R.color.default_bg_color));
            tvTitle.setTextColor(titleColor);
            int backColor = a.getColor(R.styleable.TopBar_backColor, mContext.getResources().getColor(R.color.colorPrimary));

            mToolbar.setBackgroundColor(backColor);
            String title = a.getString(R.styleable.TopBar_title);
            Drawable leftRes = a.getDrawable(R.styleable.TopBar_left_action_icon);
            mToolbar.setNavigationIcon(leftRes);
            tvTitle.setText(title);
            a.recycle();
        }

    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_topbar, this, true);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        mToolbar.setTitle("");
    }


    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public String getTitle() {
        return tvTitle.getText().toString();
    }

    public void setLeftIcon(@DrawableRes int resId) {
        mToolbar.setNavigationIcon(resId);
        mToolbar.setContentInsetStartWithNavigation(2);
    }


    public TextView getTitleView() {
        return tvTitle;
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }


    public void bind(AppCompatActivity activity) {
        activity.setSupportActionBar(mToolbar);
        activity.getSupportActionBar().setHomeButtonEnabled(false);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    public void bind(AppCompatActivity activity, boolean canBack) {

        activity.setSupportActionBar(mToolbar);
        activity.getSupportActionBar().setHomeButtonEnabled(canBack);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(canBack);



    }

}
